<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader :list="list" @addTask="addTask"></TodoHeader>
    <TodoMain :list="showList" @delFn="delTask"></TodoMain>
    <TodoFooter
      @clearFn="clearDoneFn"
      @changType="typeFn"
      :selected="sel"
      :len="showList.length"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import './styles/base.css'
import './styles/index.css'

import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  computed: {
    showList() {
      if (this.sel === 'all') {
        return this.list
      } else if (this.sel === 'yes') {
        return this.list.filter((item) => item.isDone)
      } else {
        return this.list.filter((item) => !item.isDone)
      }
    },
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('todoList')) || [],
      sel: 'all',
    }
  },
  methods: {
    addTask(task) {
      // console.log(task)
      //  数组.at('数组元素')   如果是 -1 就是数组最后一位元素
      const id = this.list.length ? this.list.at(-1).id + 1 : 100
      this.list.push({
        id,
        name: task,
        isDone: false,
      })
    },
    delTask(id) {
      console.log(id)
      this.list = this.list.filter((item) => item.id !== id)
    },
    typeFn(type) {
      this.sel = type
    },
    clearDoneFn() {
      console.log(111111111)
      this.list = this.list.filter((item) => !item.isDone)
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem('todoList', JSON.stringify(this.list))
      },
    },
  },
}
</script>
